// Run Event Studies
clear
eststo clear


* Loading vintage for each stock
use ../dta/stock_monthly_vintage.dta, clear 
keep code vintage
duplicates drop
rename code stkcd

// Merging on Market Cap and Margin Debt Data
merge 1:m stkcd using ../src/daily_margin.dta
rename stkcd code

collapse (sum) margin_hold (sum) float_cap , by(date vintage)
gen margin_cap=margin_hold/float_cap

twoway  (line margin_cap date if vintage==4, lcolor(green) lpattern(solid) lwidth(medthick)) ///
			   (line margin_cap date if vintage==3, lcolor(blue) lpattern(solid) lwidth(medthick)) ///
			   (line margin_cap date if vintage==2, lcolor(red) lpattern(solid) lwidth(medthick)) ,  ///
graphregion(color(white)) xtitle("") ytitle(Margin Debt/Market Cap)  ///
xlabel(18263 "2010" 18779 "2011" 19145 "2012" 19510 "2013" 19875 "2014" 20240 "2015")  ///
legend( on r(1) pos(6) region(lcolor(white)) size(small) label(3 "Vintage 1") label(2 "Vintage 2") label(1 "Vintage 3") order(3 2 1))   ///
ylabel(0 "0%"  0.02 "2%"  .04 "4%"  .06 "6%" .08 "8%" .10 "10%", nogrid)  ///
xline(19389, lcolor(red) lwidth(medthick))  ///
xline(19617, lcolor(blue) lwidth(medthick))  ///
xline(19988, lcolor(green) lwidth(medthick)) 

graph export ../gph/vintage_margin_to_cap.pdf, replace font(palatino)
